package com.bblocks.auth.service.impl;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bblocks.auth.mapper.UsersMapper;
import com.bblocks.auth.pojo.entity.Users;
import com.bblocks.auth.service.UsersService;
import com.bblocks.auth.utils.SelfRedisUtil;
import com.bblocks.enums.AppPlatformTypeEnum;
import com.bblocks.enums.AppTypeEnum;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2019-07-27
 */
@Slf4j
@Service("usersService")
//@Transactional
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

	public static final String SYSTEM_PUBLIC_USER_ROLEID_KEY = "YZ_SYS_PUBLIC_ROLEID";


	@Lazy
	@Autowired
	private UsersService usersService;
	@Resource
	private UsersMapper usersMapper;
	//@Autowired
	private static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(6);
	@Autowired
	private SelfRedisUtil selfRedisUtil;
	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	/**
	 * 电商相关资源
	 */
	@Value("${YZ_MALL}")
	private String yzMall;
	@Value("${YZ_MALL_LOGIN:/oauth2/oauth/token}")
	private String yzMallLogin;
	@Value("${YZ_MALL_REGISTER:/quickregister}")
	private String yzMallRegister;
	/**
	 * 登录是否需要修正im账户，默认不需要
	 */
	@Value("${YZ_LOGIN_REPAIR_IM:0}")
	private Integer repairImAcc;
	/**
	 * mall登录token最小有效期：2592000（30天）
	 */                         //2832878
	@Value("${YZ_LOGIN_EXPIRES:2592000}")
	private Long loginExpires;


	private static final String token_pre = "mallToken::";



	@Override
	public boolean isBindUserPhone(String userPhone) {
		Users users = new Users();
		users.setUserPhone(userPhone);
		Users usersModel = this.getOne(new LambdaQueryWrapper<Users>(users));
		if (null != usersModel && StrUtil.isNotEmpty(usersModel.getWxAppOpenId())) {
			return true;
		}
		return false;
	}


	public Users login(Users user, AppTypeEnum type, AppPlatformTypeEnum platformTypeEnum, String psw) {

		return user;
	}


	public Users getByUserPhone(String userPhone) {
		Users p = new Users();
		p.setUserPhone(userPhone);
		return usersMapper.selectOne(new LambdaQueryWrapper<>(p));
		//return usersMapper.getByUserPhone(userPhone);
	}

	public Users getByName(String name) {
		Users p = new Users();
		p.setName(name);
		return usersMapper.selectOne(new LambdaQueryWrapper<>(p));
		//return usersMapper.getByUserPhone(userPhone);
	}

	@Transactional
	public int updateUserOpenid(Long id, Integer userType, String openId) {
		Users users = new Users();
		users.setId(id);
		if (userType == 0) {
			users.setWxOpenId(openId);
		} else if (userType == 0) {
			users.setSbOpenId(openId);
		}

		return this.baseMapper.updateById(users);
	}

	/**
	 * 重新注册IM(异步)
	 *
	 * @param userId
	 */








}
